home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJLSR111.ZIP / libsrc / c / gen / sleep.c < prev    next >
C/C++ Source or Header  |  1993-10-01  |  936b  |  49 lines

  1. /*
  2.  * Copyright (C) 1993 Chris Boucher, ccb@soton.ac.uk.
  3.  *
  4.  * This software may be used freely so long as this copyright notice is
  5.  * left intact. There is no warranty on the software.
  6.  */
  7.  
  8. #include <sys/time.h>
  9.  
  10. sleep(unsigned int seconds)
  11. {
  12.     struct timeval a, b, r;
  13.  
  14.     gettimeofday(&a, NULL);
  15.     while (1) {
  16.         gettimeofday(&b, NULL);
  17.         etime(a, b, &r);
  18.         if (r.tv_sec >= (long)seconds)
  19.             return 0;
  20.     }
  21. }
  22.  
  23. usleep(unsigned int useconds)
  24. {
  25.     struct timeval a, b, r;
  26.  
  27.     gettimeofday(&a, NULL);
  28.     while (1) {
  29.         gettimeofday(&b, NULL);
  30.         etime(a, b, &r);
  31.         if (((r.tv_sec * 1000000) + r.tv_usec) >= (long)useconds)
  32.             return 0;
  33.     }
  34. }
  35.  
  36. static void etime(struct timeval a, struct timeval b, struct timeval *r)
  37. {
  38.     r->tv_sec = b.tv_sec - a.tv_sec;
  39.     r->tv_usec = b.tv_usec - a.tv_usec;
  40.  
  41.     if (r->tv_usec < 0) {
  42.         long t = 1 - (r->tv_usec / 1000000);
  43.         r->tv_sec -= t;
  44.         r->tv_usec += t * 1000000;
  45.     }
  46.  
  47. }
  48.  
  49.